from pymongo import MongoClient
from bson.code import Code
client = MongoClient('mongodb://ecp:ecp123456@134.175.112.213:27017/ecp_db')
db = client['ecp_db']

result = db['product_list'].find({'product_price':{'$lt':10,'$gt':3},'product_reviews':{'$gt':1,'$lt':1000},'product_rating':{'$gt':3.5}})
i = 0
while True:
  try:
    product = result.next()
    if product is None:
      break
    i = i + 1
    print("do result for " + str(i))
    if 'product_img' in product:
      aProduct = {
        "_id":product["_id"],
        "product_img" : product["product_img"],
        "product_name" : product["product_name"],
        "product_price" : product["product_price"],
        "product_url" : product["product_url"],
        "product_rating" : product["product_rating"],
        "product_reviews" : product["product_reviews"]
      }
    
      if "cate_ref" in product:
        aProduct["cate_ref"] = product["cate_ref"]
      if "cate_top" in product:
        aProduct["cate_top"] = product["cate_top"][0]
      if "bestsellers_rank" in product:
        for key in product["bestsellers_rank"].keys():
          aProduct["cate_ref_key"] = []
          aProduct["cate_ref_key"].append(key)
      db['a1_product'].update_one({'_id':product["_id"]},{'$set':aProduct},upsert=True)
  except StopIteration:
    # 遇到StopIteration就退出循环
    break

client.close()
print("finish...,totoal" + str(i))